<?php
/**
 * 控制器公共类(基础类)文件
 */

namespace app\admin\controller;

use think\Controller;
use think\Request;

/**
 * 控制器基础类
 *
 * @category Admin
 * @package  Controller
 * @author   huwhois <huwhois@163.com>
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link     http://www.hashbangcode.com/
 */

class Base extends Controller
{
    protected $user_id;
    protected $user_name;
    protected $user_role_id;
    
    /**
     * 判断是否登陆，检查登陆者是否有权调用想应的控制器和操作
     * $request Tp5.0 的请求响应对象
     */
    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        //判断是否登陆
        if (!session('user_id')) {
            $this->error('请登陆', 'login/index');
        }
        
        $this->user_id = session('user_id');
        $this->user_name = session('user_name');
        $this->user_role_id = session('user_role_id');

        $user_role_name = model('Adminrole')->getAdminroleName($this->user_role_id);

        $c = strtolower(request()->controller());
        $a = strtolower(request()->action());
        
        //权限检查,针对角色
        if (!$this->checkAuthor($this->user_role_id, $c, $a)) {
            $this->error('你无权限操作', 'index/index');
        }
        
        //其他的controller就不要再用这几个变量名
        $this->assign("user_role_name", $user_role_name);
        $this->assign("user_name", $this->user_name);
        $this->assign("user_id", $this->user_id);
        $this->assign('controller', $c);
        $this->assign('action', $a);

        //记录日志
        //$this->addLog();
    }

     /**
      * 权限检查
     */
    private function checkAuthor($user_role_id, $c, $a)
    {
        if (!$user_role_id) {
            return false;
        }
        //超级管理员不检查
        if ($user_role_id == 1) {
            return true;
        }

        if (preg_match('/^public_/', $a)) {
            return true;
        }

        if ($c == 'index' && $a == 'index') {
            return true;
        }

        $per_ids = model('Adminrole')->getAdminrolePermi($this->user_role_id);

        $datatest = model('Permission')->field('id,pid,permi_method')->select($per_ids);
        $arr = array();
        $actions = array();
        foreach ($datatest as $val) {
            if ($val->pid==0) {
                $arr[] = $val->permi_method.'/index';
            } else {
                $arr[] = model('Permission')->getPermitCrtl($val->pid).'/'.$val->permi_method;
            }
        }
        // var_dump($arr);
        // var_dump($c.'/'.$a);exit;
        if (in_array($c.'/'.$a, $arr)) {
            return true;
        }

        return false;
    }

    /**
     * 记录日志
     */
    private function addLog()
    {

        $data = array();
        $data['querystring'] = request()->query()?'?'.request()->query():'';
        $data['m'] = request()->module();
        $data['c'] = request()->controller();
        $data['a'] = request()->action();
        $data['userid'] = $this->user_id;
        $data['username'] = $this->user_name;
        $data['ip'] = ip2long(request()->ip());
        $arr = array('Index/index','Log/index','Menu/index');
        if (!in_array($data['c'].'/'.$data['a'], $arr)) {
            db('admin_log')->insert($data);
        }
    }
}
